package com.up.vms.application.service;

import com.up.vms.domain.module.UserDTO;
import com.up.vms.domain.vm.entity.SystemUser;
import com.up.vms.domain.vm.enums.AdminRespCode;
import com.up.vms.domain.vm.repository.SystemUserRepository;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SystemUserService {

    @Autowired
    private SystemUserRepository systemUserRepository;

    public GeneRespDTO createUser(SystemUser systemUserDTO) {

        SystemUser systemUser = systemUserRepository.findByUsername(systemUserDTO.getUsername());

        if (CommonUtils.isNotNull(systemUser)) {
            if (CommonUtils.isNotNull(systemUserDTO.getPassword())) {
                systemUser.setPassword(systemUserDTO.getPassword());
            }
            systemUser.setNickName(systemUserDTO.getNickName());
            systemUser.setStatus(systemUserDTO.getStatus());
            systemUser.setModified(new Date());
            systemUser = systemUserRepository.save(systemUser);
        } else {
            systemUser = systemUserRepository.save(systemUserDTO);
        }

        log.info("用户保存结果:{}", systemUser.getId());

        GeneRespDTO geneRespDTO = new GeneRespDTO(new RespCode(ReturnCode.SUCCESS));
        Map map = new HashMap();
        map.put("id", systemUser.getId());
        geneRespDTO.setData(map);
        return geneRespDTO;
    }

    public GeneRespDTO<UserDTO> getUserModelByUsername(String username) {
        SystemUser systemUser = systemUserRepository.findByUsername(username);

        if (null == systemUser || systemUser.getStatus() < 0) {
            // 解决用户不存在的情况
            return new GeneRespDTO(new RespCode(AdminRespCode.FIND_NO_USER.code, AdminRespCode.FIND_NO_USER.msg));
        }
//
//        Long roleId = systemUser.getRoleId();
//
//        List<SystemRoleMenu> systemRoleMenus = systemRoleMenuMapper.selectByRoleId(roleId);
//        if (CollectionUtils.isEmpty(systemRoleMenus)) {
//            // 绑定角色异常
//            return new GeneralServiceResp<>(new ResponseCode(AdminRespCode.FIND_NO_MENU.code, AdminRespCode.FIND_NO_MENU.msg));
//        }
//
//        SystemUserGroup systemUserGroup = systemUserGroupMapper.selectByPrimaryKey(systemUser.getGroupId());
//        if (null == systemUserGroup) {
//            return new GeneralServiceResp<>(new ResponseCode(AdminRespCode.FIND_NO_USER_GROUP.code, AdminRespCode.FIND_NO_USER_GROUP.msg));
//        }
//
//        roleId = systemUserGroup.getRoleId();
//        if (null == roleId) {
//            return new GeneralServiceResp<>(new ResponseCode(AdminRespCode.FIND_NO_ROLE.code, AdminRespCode.FIND_NO_ROLE.msg));
//        }
//
//        SystemRole systemRole = systemRoleMapper.selectByPrimaryKey(roleId);
//        if (null == systemRole) {
//            return new GeneralServiceResp<>(new ResponseCode(AdminRespCode.FIND_NO_ROLE.code, AdminRespCode.FIND_NO_ROLE.msg));
//        }

//        List<Long> menuIds = systemRoleMenus.stream().map(SystemRoleMenu::getMenuId).collect(Collectors.toList());
//
        UserDTO userDTO = new UserDTO();
//        userModel.setMenuIds(menuIds);
        userDTO.setUserId(systemUser.getId());
        userDTO.setUsername(username);
//        userModel.setRoleId(roleId);
//        userModel.setRoleCode(systemRole.getRoleCode());
        userDTO.setPassword(systemUser.getPassword());
        userDTO.setStatus(systemUser.getStatus());
//        Long organId = systemUserGroup.getOrganId();
//        if (organId != null) {
//            GeneralServiceResp<Organization> generalServiceResp = organService.getOrganById(organId);
//            if (generalServiceResp != null) {
//                Organization organization = generalServiceResp.getData();
//                userModel.setOrganName(organization.getOrganName());
//            }
//        }
//        userModel.setOrganId(organId);


        return new GeneRespDTO(new RespCode(ReturnCode.SUCCESS), userDTO);

    }
}
